package com.cyz.admin.controller;

import com.cyz.common.dto.ButtonDto;
import com.cyz.common.dto.ButtonGroupDto;
import com.cyz.admin.service.ButtonService;
import com.cyz.admin.service.ButtongroupService;
import com.cyz.common.utils.R;
import com.cyz.common.validated.AddGroup;
import com.cyz.common.validated.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "管理端按钮相关接口")
@RestController
@RequestMapping("/button")
public class ButtonController {

    @Autowired
    private ButtonService buttonService;
    @Autowired
    private ButtongroupService buttongroupService;

    @PreAuthorize("@ps.hasPermission('contant:free:button')")
    @ApiOperation(value = "查询免费课程按钮")
    @GetMapping("/queryFreeButtons")
    public R queryFreeButtons(){
        return buttonService.queryFreeButtons();
    }

    @PreAuthorize("@ps.hasPermission('contant:free:buttonType')")
    @ApiOperation(value = "查询免费课程按钮分类")
    @GetMapping("/queryFreeButtonGroups")
    public R queryFreeButtonGroups() {
        return buttongroupService.queryFreeButtonGroups();
    }

    @PreAuthorize("@ps.hasPermission('contant:free:buttonType')")
    @ApiOperation(value = "查询免费课程按钮分类及其包含的按钮")
    @GetMapping("/queryFreeButtonGroupsAndButtons")
    public R queryFreeButtonGroupsAndButtons() {
        return buttongroupService.queryFreeButtonGroupsAndButtons();
    }

    @PreAuthorize("@ps.hasPermission('contant:free:button')")
    @ApiOperation(value = "删除免费课程按钮")
    @DeleteMapping("/deleteFreeButtons")
    public R deleteFreeButtons(Integer[] ids){
        return buttonService.deleteFreeButtons(ids);
    }

    @PreAuthorize("@ps.hasPermission('contant:free:buttonType')")
    @ApiOperation(value = "删除免费课程按钮分类")
    @DeleteMapping("/deleteFreeButtonGroups")
    public R deleteFreeButtonGroups(Integer[] ids) {
        return buttongroupService.deleteFreeButtonGroups(ids);
    }

    @PreAuthorize("@ps.hasPermission('contant:free:button')")
    @ApiOperation(value = "增加免费课程按钮")
    @PostMapping("/addFreeButton")
    public R addFreeButton(@Validated(AddGroup.class) @RequestBody ButtonDto buttonDto){
        return buttonService.addFreeButton(buttonDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:free:buttonType')")
    @ApiOperation(value = "增加免费课程按钮分类")
    @PostMapping("/addFreeButtonGroup")
    public R addFreeButtonGroup(@Validated(AddGroup.class) @RequestBody ButtonGroupDto buttonGroupDto) {
        return buttongroupService.addFreeButtonGroup(buttonGroupDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:free:button')")
    @ApiOperation(value = "更新免费课程按钮")
    @PutMapping("/updateFreeButton")
    public R updateFreeButton(@Validated(UpdateGroup.class) @RequestBody ButtonDto buttonDto){
        return buttonService.updateFreeButton(buttonDto);
    }

    @PreAuthorize("@ps.hasPermission('contant:free:buttonType')")
    @ApiOperation(value = "更新免费课程按钮分类")
    @PutMapping("/updateFreeButtonGroup")
    public R updateFreeButtonGroup(@Validated(UpdateGroup.class) @RequestBody ButtonGroupDto buttonGroupDto) {
        return buttongroupService.updateFreeButtonGroup(buttonGroupDto);
    }
}
